package com.yanqu.road.server.cmd.banquet;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.UserBanquetData;
import com.yanqu.road.entity.banquet.UserCrossBanquetData;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.BanquetMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

//@Cmd(code = Protocol.S_BANQUET_PLAYER_GET, desc = "请求用戶宴会(用户头像直接进去用来请求的？)")
public class GetPlayerBanquetCmd extends AbstractCommand {

	private long userId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		BanquetProto.PlayerBanquetReq reqMsg = BanquetProto.PlayerBanquetReq.parseFrom(packet.getBytes());
		userId = reqMsg.getUserId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

	}

	@Override
	public void sendData(GamePlayer player) {
		//两个宴会，一个本服私宴一个跨服私宴
		BanquetProto.PlayerBanquetResp.Builder respMsg = BanquetProto.PlayerBanquetResp.newBuilder();
		UserBanquetData userBanquetData = BanquetMgr.getUserBanquet(userId);
		if(userBanquetData != null){
			boolean selfJoin = BanquetMgr.isSelfJoin(player.getUserId(), userBanquetData.getBanquetId());
			BanquetProto.PlayerBanquetTemp.Builder respServerTemp = BanquetProto.PlayerBanquetTemp.newBuilder();
			respServerTemp.setBanquetId(userBanquetData.getBanquetId());
			respServerTemp.setSelfJoin(selfJoin);
			BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(userBanquetData.getType());
			respServerTemp.setBanquetType(banquetInfo.getType1());
			respMsg.addBanquets(respServerTemp);
		}
		UserCrossBanquetData userCrossBanquetData = CrossBanquetMgr.getUserBanquet(userId);
		if(userCrossBanquetData != null){
			boolean selfJoin = CrossBanquetMgr.isSelfJoin(player.getUserId(), userCrossBanquetData.getBanquetId());
			BanquetProto.PlayerBanquetTemp.Builder respCrossTemp = BanquetProto.PlayerBanquetTemp.newBuilder();
			respCrossTemp.setBanquetId(userCrossBanquetData.getBanquetId());
			respCrossTemp.setSelfJoin(selfJoin);
			BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(userCrossBanquetData.getType());
			respCrossTemp.setBanquetType(banquetInfo.getType1());
			respMsg.addBanquets(respCrossTemp);
		}
		if(respMsg.getBanquetsList().size() == 0){
			ret = GameErrorCode.E_BANQUET_NORMAL_PLAYER_NO;
		}
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_BANQUET_PLAYER_GET, respMsg);
	}
}
